package com.xsq.audio.player.utils;

import android.support.v4.media.session.PlaybackStateCompat;
import android.widget.ImageView;
import com.xsq.audio.R;
import com.xsq.audio.player.PlayManager;
import com.xsq.audio.player.playlist.PlayListManager;

/**
 * Created by Shiquan.Xiao on 2023/3/31.
 */
public class UIUtils {

    private static long lastClickTime = 0;

    public static boolean isFastClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    /**
     * 改变播放模式
     */
    public static void updatePlayMode(ImageView imageView, boolean isChange) {
        if (imageView == null) return;
        int playMode = PlayListManager.INSTANCE.getPlayModeId();
        if (isChange) playMode = PlayListManager.INSTANCE.updatePlayMode();
        switch (playMode) {
            case PlayListManager.PLAY_MODE_LOOP:
                imageView.setImageResource(R.drawable.ic_repeat);
                PlayManager.setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE);
                PlayManager.setRepeatMode(PlaybackStateCompat.REPEAT_MODE_NONE);
                if (isChange)
                    ToastUtils.show(imageView.getContext(), imageView.getResources().getString(R.string.play_mode_loop));
                break;
            case PlayListManager.PLAY_MODE_REPEAT:
                PlayManager.setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE);
                PlayManager.setRepeatMode(PlaybackStateCompat.REPEAT_MODE_ONE);
                imageView.setImageResource(R.drawable.ic_repeat_one);
                if (isChange)
                    ToastUtils.show(imageView.getContext(), imageView.getResources().getString(R.string.play_mode_repeat));
                break;
            case PlayListManager.PLAY_MODE_RANDOM: {
                PlayManager.setRepeatMode(PlaybackStateCompat.REPEAT_MODE_ALL);
                PlayManager.setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_ALL);
                imageView.setImageResource(R.drawable.ic_shuffle);
                if (isChange)
                    ToastUtils.show(imageView.getContext(), imageView.getResources().getString(R.string.play_mode_random));
                break;
            }
        }
    }

    public static void updatePlayModeIcon(ImageView imageView) {
        if (imageView == null) return;
        int playMode = PlayListManager.INSTANCE.getPlayModeId();
        switch (playMode) {
            case PlayListManager.PLAY_MODE_LOOP:
                imageView.setImageResource(R.drawable.ic_repeat);
                break;
            case PlayListManager.PLAY_MODE_REPEAT:
                imageView.setImageResource(R.drawable.ic_repeat_one);
                break;
            case PlayListManager.PLAY_MODE_RANDOM: {
                imageView.setImageResource(R.drawable.ic_shuffle);
                break;
            }
        }
    }

}
